package com.lumyc.springbootaop.advice;

import com.lumyc.springbootaop.annotation.LogTrace;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName MyAdvice
 * @Description TODO
 * @Author lumyc
 * @Date 2023/2/23 9:51
 * @Version 1.0
 */

@Component
@Aspect
@Slf4j
public class MyAdvice {



    @Pointcut("execution(* com.lumyc.springbootaop.controller.*.hello(..))")
    public void myPointcut() {

    }

    @Around("myPointcut()")
    public Object myAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("前置通知！");
        joinPoint.proceed();
        log.info("后置通知！");
        return "通知！";
    }

    @Around("@annotation(com.lumyc.springbootaop.annotation.LogTrace)")
    public String addLog(ProceedingJoinPoint joinPoint) throws Throwable {

        Method method = AspectUtil.getMethod(joinPoint);
        LogTrace logTrace = method.getAnnotation(LogTrace.class);
        if(logTrace.value()){
            log.info("执行通知！");
        }else{
            log.info("不执行通知！");
        }
        joinPoint.proceed();

        return "我是通过注解注入的！";
    }
}
